/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.ctrl;

import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.material.Material;
import com.jme3.material.RenderState;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import com.jme3.scene.Node;
import java.io.IOException;
import trailing.TrailingObject;

/**
 *
 * @author JhonKkk
 */
public class TrailingCtrl extends AbstractControl {
    private Spatial mTrailingSpatial;

    /**
     * @return the trailingTagName
     */
    public String getTrailingTagName() {
        return trailingTagName;
    }

    /**
     * @param trailingTagName the trailingTagName to set
     */
    public void setTrailingTagName(String trailingTagName) {
        this.trailingTagName = trailingTagName;
    }
    private String trailingTagName = "trailingNode";

    /**
     * @return the enableTrailing
     */
    public boolean isEnableTrailing() {
        return enableTrailing;
    }

    /**
     * @param enableTrailing the enableTrailing to set
     */
    public void setEnableTrailing(boolean enableTrailing) {
        this.enableTrailing = enableTrailing;
    }

    /**
     * @return the length
     */
    public float getLength() {
        return length;
    }

    /**
     * @param length the length to set
     */
    public void setLength(float length) {
        this.length = length;
    }

    /**
     * @return the trailingMaterial
     */
    public Material getTrailingMaterial() {
        return trailingMaterial;
    }

    /**
     * @param trailingMaterial the trailingMaterial to set
     */
    public void setTrailingMaterial(Material trailingMaterial) {
        this.trailingMaterial = trailingMaterial;
    }
    //Any local variables should be encapsulated by getters/setters so they
    //appear in the SDK properties window and can be edited.
    //Right-click a local variable to encapsulate it with getters and setters.
    private TrailingObject mTrailingObject;
    private Material trailingMaterial;
    private boolean mIsFirst = true;
    private boolean enableTrailing = false;
    private float length = 5.0f;
    public TrailingCtrl(){
        mTrailingObject = new TrailingObject();
        mTrailingObject.setTagSpatial(spatial);
        mTrailingObject.setup();
    }

    @Override
    protected void controlUpdate(float tpf) {
        //TODO: add code that controls Spatial,
        //e.g. spatial.rotate(tpf,tpf,tpf);
        if(trailingMaterial != null && mIsFirst){
            mIsFirst = false;
            mTrailingObject.mTrailingGeometry.setMaterial(trailingMaterial);
            trailingMaterial.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
            trailingMaterial.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
            ((Node)spatial).attachChild(mTrailingObject.mTrailingGeometry);
            if(mTrailingSpatial == null){
                mTrailingSpatial = ((Node)spatial).getChild(trailingTagName);
            }
        }
        if(trailingMaterial != null && enableTrailing){
            mTrailingObject.setTagSpatial(mTrailingSpatial);
//            System.out.println("localtion:" + spatial.getLocalTranslation());
            mTrailingObject.setTrailingVec(spatial.getLocalTranslation().add(spatial.getLocalRotation().getRotationColumn(1).mult(length)), spatial.getLocalTranslation());
//            mTrailingObject.setTrailingVec(new Vector3f(0, 3, 0), new Vector3f(0, 0, 0));
            mTrailingObject.setTrailing(true);
            //开始采样
            mTrailingObject.sampling((long) (tpf * 1000L));
        }
    }
    
    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
        //Only needed for rendering-related operations,
        //not called when spatial is culled.
    }
    
    public Control cloneForSpatial(Spatial spatial) {
        TrailingCtrl control = new TrailingCtrl();
        //TODO: copy parameters to new Control
        return control;
    }
    
    @Override
    public void read(JmeImporter im) throws IOException {
        super.read(im);
        InputCapsule in = im.getCapsule(this);
        trailingMaterial = (Material) in.readSavable("trailingMaterial", null);
        setEnableTrailing(in.readBoolean("enableTrailing", false));
        length = in.readFloat("length", 5.0f);
        trailingTagName = in.readString("trailingTagName", null);
        //TODO: load properties of this Control, e.g.
        //this.value = in.readFloat("name", defaultValue);
    }
    
    @Override
    public void write(JmeExporter ex) throws IOException {
        super.write(ex);
        OutputCapsule out = ex.getCapsule(this);
        out.write(trailingMaterial, "trailingMaterial", null);
        out.write(length, "length", 5.0f);
        out.write(isEnableTrailing(), "enableTrailing", false);
        out.write(trailingTagName, "trailingTagName", "trailingNode");
        //记得删掉,否则会重复写入这个对象
        if(mTrailingObject.mTrailingGeometry.getParent() != null){
            ((Node)spatial).detachChild(mTrailingObject.mTrailingGeometry);
        }
        //TODO: save properties of this Control, e.g.
        //out.write(this.value, "name", defaultValue);
    }
    
}
